//
//  RegisterNextViewController.m
//  cherryproduct
//
//  Created by zou shigang on 14/10/26.
//  Copyright (c) 2014年 cherry. All rights reserved.
//

#import "RegisterNextViewController.h"

@interface RegisterNextViewController ()

@end

@implementation RegisterNextViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"设置密码"];
    self.myView.hidden=YES;
    
    self.text_pass_0.secureTextEntry=YES;
    self.text_pass_1.secureTextEntry=YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.text_pass_0 resignFirstResponder];
    [self.text_pass_1 resignFirstResponder];
}

- (IBAction)doActionOver:(id)sender {
    NSString * str_pwd = self.text_pass_0.text;
    NSString * str_repwd = self.text_pass_1.text;
    
    if (str_pwd == nil || [str_pwd isEqualToString:@""]) {
        [self showAlertView:@"温馨提示" subtitle:@"请输入密码!"];
        return;
    }
    if (str_repwd == nil || [str_repwd isEqualToString:@""]) {
        [self showAlertView:@"温馨提示" subtitle:@"请输入确认密码!"];
        return;
    }
    if (![str_pwd isEqualToString:str_repwd]) {
        [self showAlertView:@"温馨提示" subtitle:@"两次输入密码不一样!"];
        return;
    }
    
    //...
    NSMutableDictionary * requestDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                                        str_pwd, @"pwd",
                                        str_repwd, @"repwd", nil];
    
    [self.view startLoadingAnimation];
    
    [[HttpNewBaseService sharedClient] postRequestOperation:@"member/register_phone"
                                            paramDictionary:requestDic
                                               onCompletion:^(NSString *responseString)
     {
         [self.view stopLoadingAnimation];
         
         @try {
             NSDictionary * dic = [responseString JSONValue];
             if (dic && [dic isKindOfClass:[NSDictionary class]]) {
                 
                 NSInteger status = [[dic objectForKey:@"status"] integerValue];
                 
                 if (status == 1) {
                     {
                         //保存cookie
                         NSHTTPCookieStorage * cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];
                         NSData *data = [NSKeyedArchiver archivedDataWithRootObject:[cookieJar cookies]];
                         [[NSUserDefaults standardUserDefaults] setObject:data forKey:kUserDefaultsCookie];
                     }
                     
                     {//保存登录信息
                         //登陆成功
                         NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];
                         [defaults setObject:self.str_userid forKey:@"USERNAME"];
                         [defaults setObject:str_pwd forKey:@"USERPASSWORD"];
                         [defaults synchronize];
                         
                         {
                             NSDictionary * data_dic = [dic objectForKey:@"data"];
                             [[LoginViewController shareUserInfo] setDictionary:data_dic];
                             [[LoginViewController shareUserInfo] setObject:@"1" forKey:KEY_HAVE_LOGIN];
                             
                             {
                                 NSString * logo = [data_dic objectForKey:@"logo"];
                                 if (logo) {
                                     NSString * new_logo = [NSString stringWithFormat:@"%@%@",JPG,logo];
                                     //保存login img
                                     [[LoginViewController shareUserInfo] setObject:new_logo forKey:@"new_logo"];
                                 }
                                 NSString * cover = [data_dic objectForKey:@"cover"];
                                 if (cover) {
                                     NSString * pp = [cover stringByReplacingOccurrencesOfString:@"Attachment/" withString:@""];
                                     NSString * new_cover = [NSString stringWithFormat:@"%@%@",JPG,pp];
                                     //保存login img
                                     [[LoginViewController shareUserInfo] setObject:new_cover forKey:@"new_cover"];
                                 }
                             }
                             [LoginViewController saveUserInfo];
                         }
                     }
                     
                     __block URBAlertView *alertView = [URBAlertView dialogWithTitle:@"温馨提示" subtitle:@"注册成功"];
                     alertView.blurBackground = YES;
                     [alertView addButtonWithTitle:@"确定"];
                     [alertView setHandlerBlock:^(NSInteger buttonIndex, URBAlertView *alertView) {
                         NSLog(@"button tapped: index=%i", buttonIndex);
                         [alertView hideWithCompletionBlock:^{
                             // stub
                             [self.navigationController popToRootViewControllerAnimated:YES];
                         }];
                     }];
                     [alertView showWithAnimation:URBAlertAnimationFlipHorizontal];
                 } else {
                     [self showAlertView:@"温馨提示" subtitle:@"注册失败!"];
                 }
             }
         } @catch (NSException *exception) {
             [self showAlertView:@"温馨提示" subtitle:@"注册失败!"];
         }
         
     } onError:^(NSError *error) {
         [self.view stopLoadingAnimation];
         
         [self showAlertView:@"温馨提示" subtitle:@"网络断了!"];
     } onAnimated:YES];
}


@end

























